package com.neulogistics.core.customercenter.infrastructure.persistence.converter;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.neulogistics.core.customercenter.domain.model.aggregate.Customer;
import com.neulogistics.core.customercenter.domain.model.valueobject.Address;
import com.neulogistics.core.customercenter.infrastructure.persistence.orm.po.CustomerPO;
import org.springframework.beans.BeanUtils;

public class CustomerEntityPOConverter {
    public static CustomerPO entityToPO(Customer customer){
        if(customer == null)
            return null;

        CustomerPO customerPO = new CustomerPO();
        // 复制普通属性
        BeanUtils.copyProperties(customer, customerPO);

        // 地址值对象转换为json对象
        if(customer.getReceiverAddress() != null){
            customerPO.setReceiverAddress(JSONUtil.parseObj(customer.getReceiverAddress()));
        }
        return customerPO;
    }

    public static Customer POToEntity(CustomerPO customerPO){
        if(customerPO == null)
            return null;

        Customer customer = new Customer();
        // 复制普通属性, id是可以在这里被复制进去的
        BeanUtils.copyProperties(customerPO, customer);
        // json对象转换为值对象
        customer.setReceiverAddress(JSONUtil.toBean(customerPO.getReceiverAddress(), Address.class));
        return customer;
    }

}
